package smooth;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;


/**
 * This class enclose some functions to process text data
 * It opens a buffer reader pointing to a file which path  is specified by the user
 * methods to close and read lines are provided.
 * @author Sergio Duarte
 *
 */
public class FileInput implements Constants {

	
	private BufferedReader reader;

	private String filename;

	private boolean eof = false;

	private int window = 0;

	

	/**
	 * The constructor receive the path of the file that the use wants to read
	 */
	public FileInput(String fname) {
		filename = fname;

		try {
			reader = new BufferedReader(new FileReader(filename));
		} catch (FileNotFoundException e) {
			error("Can't open file: " + filename);
		}
	}

	
	
	/**
	 * 
	 * @return  string with the next line of the file
	 */

	public final synchronized String readString() {

		String s = "";
		try {
			s = reader.readLine();
			if (s == null) {
				eof = true;

				//return dummy word
				return DUMMY;
			}
		} catch (IOException e) {
			error("readString failed for file: " + filename);
		}
		return s;
	}

	/**
	 *  Close the file when finished
	 */
	public void close() {
		try {
			reader.close();
		} catch (IOException e) {
			error("Can't close file: " + filename);
		}
	}

	/**
	 *  Return true if the end of file has been reached.
	 */
	public boolean eof() {
		return eof;
	}

	private void error(String msg) {
		System.out.println(msg);
		System.out.println("Unable to continue executing program.");
		System.exit(0);
	}

	

}